from abc import ABC, abstractmethod
from typing import Any, Dict, Optional
from dataclasses import dataclass
from enum import Enum

class ComponentType(Enum):
    """组件类型枚举"""
    LLM = "llm"  # 大模型调用
    EMBEDDING = "embedding"  # 向量数据库
    VECTOR_DB = "vector_db"  # 向量数据库
    SQL_DB = "sql_db"  # 关系型数据库
    CUSTOM = "custom"  # 自定义组件
    HTTP = "http"

@dataclass
class ComponentConfig:
    """组件配置"""
    name: str
    type: ComponentType
    config: Dict[str, Any]
    input_schema: Dict[str, Any]  # 输入数据格式定义
    output_schema: Dict[str, Any]  # 输出数据格式定义

class BaseComponent(ABC):
    """基础组件类"""
    
    def __init__(self, config: ComponentConfig):
        self.config = config
        
    @classmethod
    def get_metadata(cls) -> Dict[str, Any]:
        """获取组件的元信息，包括类型、名称、配置schema等"""
        return {
            "type": cls.get_component_type(),
            "name": cls.get_component_name(),
            "description": cls.get_component_description(),
            "config_schema": cls.get_config_schema()
        }
    
    @classmethod
    @abstractmethod
    def get_component_type(cls) -> ComponentType:
        """获取组件类型"""
        pass
    
    @classmethod
    @abstractmethod
    def get_component_name(cls) -> str:
        """获取组件名称"""
        pass
    
    @classmethod
    @abstractmethod
    def get_component_description(cls) -> str:
        """获取组件描述"""
        pass
    
    @classmethod
    @abstractmethod
    def get_config_schema(cls) -> Dict[str, Any]:
        """获取配置schema"""
        pass
    
    @abstractmethod
    def get_input_schema(self) -> Dict[str, Any]:
        """获取输入schema"""
        pass
    
    @abstractmethod
    def get_output_schema(self) -> Dict[str, Any]:
        """获取输出schema"""
        pass
    
    @abstractmethod
    async def execute(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行组件逻辑"""
        pass
    
    @abstractmethod
    def validate_input(self, input_data: Dict[str, Any]) -> bool:
        """验证输入数据是否符合schema"""
        pass
    
    @abstractmethod
    def validate_output(self, output_data: Dict[str, Any]) -> bool:
        """验证输出数据是否符合schema"""
        pass 